let textArea1 = document.getElementById('coordinates1');
let textArea2 = document.getElementById('coordinates2');
let polyline = document.getElementById('polyline');
let polygon = document.getElementById('polygon');
let result = document.getElementById('format');
let mPolyline = document.getElementById('mPolyline');
let mPolygon = document.getElementById('mPolygon');

function initData(){
    textArea1.value = points1;
    textArea2.value = points2;
}

initData();


function transformPolyline(){
    let inputValue = textArea1.value;
    cvsToWktLine([inputValue],[',',';'])
}

function transformPolygon(){
    let inputValue = textArea1.value;
    cvsToWktPolygon([inputValue],[',',';'])
}

function transformMPolyline(){
    let inputValue1 = textArea1.value;
    let inputValue2 = textArea2.value;
    cvsToWktLine([inputValue1,inputValue2],[',',';'])
}

function transformMPolygon(){
    let inputValue1 = textArea1.value;
    let inputValue2 = textArea2.value;
    cvsToWktPolygon([inputValue1,inputValue2],[',',';'])
}

function cvsToWktLine(lines,separates){
    let formatters = [];
    lines.forEach((item)=>{
        let formatter = csvFormatter(item,separates);
        formatters.push(formatter);
    })
    let polylineStr;
    let wktType = 'LINESTRING';
    if(formatters.length === 1){
        let formatter = formatters[0];
        polylineStr = `${wktType}${formatter}`
        outputLine(polylineStr);
    }else if(formatters.length > 1){
        wktType = 'MULTILINESTRING';
        polylineStr = formatters.join(',')
        polylineStr = `${wktType}(${polylineStr})`
        outputLine2(polylineStr);
    }
    return polylineStr;
}


function cvsToWktPolygon(polygons,separates){
    let wktType = 'POLYGON';
    let formatters = [];
    polygons.forEach((polygon)=>{
        let formatter = csvFormatter(polygon,separates,'polygon');
        formatter = formatter.replace('&','),(');
        formatters.push(formatter);
    })
    let polygonStr;
    if(formatters.length === 1){
        let formatter = formatters[0];
        polygonStr = `${wktType}${formatter}`
        outputPolygon(polygonStr);
    }else if(formatters.length > 1){
        wktType = 'MULTIPOLYGON';
        polygonStr = formatters.join(',')
        polygonStr = `${wktType}(${polygonStr})`
        outputPolygon2(polygonStr);
    }
    return polygonStr;
}


function csvFormatter(value,separates,geoType){
    // 删除首尾空格
    value = value.replace(/(^\s+)|(\s+$)/g,'');
    // 换行符替换
    if(geoType === 'polygon'){
        value = value.replace(/\s*\n{2,}\s*/g,'&');
    }
    value = value.replace(/\s*\n\s*/g,'$');
    let seps = '\\s';
    separates.forEach((item)=>{
        if(item != '$' && item != '$'){
            seps += item
        }
    })
    let expStr = `[${seps}]+`;
    // 经纬度坐标分隔符处理
    let exp = new RegExp(expStr,'g');
    value = value.replace(exp,' ');
    value = value.replace(/\$/g,',');
    if(geoType === 'polygon'){
        value = value.replace('&','),(');
        value = `((${value}))`;
    }else{
        value = `(${value})`
    }
    output(value)
    return value;
}

function output(value){
    result.innerHTML = value.replace(/\s/g,'&nbsp');
}

function outputLine(value){
    polyline.innerHTML = value.replace(/\s/g,'&nbsp');
}

function outputPolygon(value){
    polygon.innerHTML = value.replace(/\s/g,'&nbsp');
}

function outputLine2(value){
    mPolyline.innerHTML = value.replace(/\s/g,'&nbsp');
}

function outputPolygon2(value){
    mPolygon.innerHTML = value.replace(/\s/g,'&nbsp');
}
